<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Admin\Controller\AdminController;
class LinkController extends AdminController
{
    function _initialize() {
        parent::_initialize();
        $this->assign('action', 'link');
        $this->assign('sys', 'website'); // 数据集
    }
    // 友情链接
    public function index(){
        $link = M('Link');
        if(IS_POST){
            $user_name = I('get.user_name');
            $datas['user_name'] = array('like',"%link_title%");
        }
        $count = $link->where($datas)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $res = $link->where($datas)->order('link_sort')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('res',$res);// 赋值数据集
        $this->assign('pages',$show);// 赋值分页输出

        $this->display();
    }

    // 新增
    public function add(){
        if(IS_POST){
            $datas = I('post.');
            $link = M('Link');
            $res = $link->data($datas)->add();
            if($res){
                $this->success('新增成功', U('index'));
            }else{
                $this->error('新增失败');
            }
        }

        $this->display();
    }

    // 编辑
    public function save(){
        $id['link_id'] = I('get.link_id');
        $link = M('Link');
        $res = $link->where($id)->find();
        $this->assign('res',$res);
        if(IS_POST){
            $datas = I('post.');
            $link = M('Link');
            $res = $link->where($id)->data($datas)->save();
            $this->success('修改成功', U('index'));
        }

        $this->display();
    }

    // 删除
    public function del(){
        $id['link_id'] = I('get.link_id');
        $link = M('Link');
        $res = $link->where($id)->delete();
        $this->success('删除成功', U('index'));
    }
}
